package com.web.exiaodao.remote.service.impl;

import com.web.exiaodao.business.constant.JConstant;
import com.web.exiaodao.business.entity.News;
import com.web.exiaodao.remote.vo.NewsVo;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.repository.NewsRepository;
import com.web.exiaodao.remote.service.ApiNewsService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;

/**
 * @author: zhangl
 * @times: 2015-5-15下午06:46:35
 * 类的说明：前端APINews接口实现类
 **/
@Service("ApiNewsService")
@Transactional(readOnly = false)
public class ApiNewsServiceImpl extends BaseService<News> implements ApiNewsService{

    @Autowired
    NewsRepository newsRepository;

    @Override
    public XaResult<NewsVo> findOne(Long tId) throws BusinessException {
        News obj = newsRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
        XaResult<NewsVo> xr = new XaResult<NewsVo>();
        if (XaUtil.isNotEmpty(obj)) {
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj),NewsVo.class));
            xr.getObject().setId(obj.getId());
            xr.getObject().setContent(JConstant.h5Header + xr.getObject().getContent());
        } else {
            throw new BusinessException(XaConstant.Message.object_not_find);
        }
        return xr;
    }

    @Override
    public XaResult<List<NewsVo>> findListNEStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if(status == null){// 默认显示非删除的所有数据
            status = XaConstant.Status.delete;
        }
        filters.put("status", new SearchFilter("status", Operator.NE, status));
        Page<News> page = newsRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), News.class), pageable);
        XaResult<List<NewsVo>> xr = new XaResult<List<NewsVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), NewsVo.class));
        for(int i=0;i<page.getContent().size();i++){
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
        }
        return xr;
    }

    @Override
    public XaResult<List<NewsVo>> findListEQStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if(status == null){// 默认显示正常数据
            status = XaConstant.Status.valid;
        }
        filters.put("status", new SearchFilter("status", Operator.EQ, status));
        Page<News> page = newsRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), News.class), pageable);
        XaResult<List<NewsVo>> xr = new XaResult<List<NewsVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), NewsVo.class));
        for(int i=0;i<page.getContent().size();i++){
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
            xr.getObject().get(i).setContent("");
        }
        return xr;
    }

    @Override
    public XaResult<NewsVo> multiOperate(String modelIds,
                                         Integer status) throws BusinessException {
        XaResult<NewsVo> xr = new XaResult<NewsVo>();
        if(status == null){
            status = XaConstant.Status.delete;
        }
        if(modelIds != null){
            String[] ids = modelIds.split(",");
            for(String id : ids){
                News obj = newsRepository.findByIdAndStatusNot(Long.parseLong(id),status);
                if (XaUtil.isNotEmpty(obj)) {
                    obj.setStatus(status);
                    obj = newsRepository.save(obj);
                    xr.setObject(JSON.parseObject(JSON.toJSONString(obj), NewsVo.class));
                    xr.getObject().setId(obj.getId());
                } else {
                    throw new BusinessException(XaConstant.Message.object_not_find);
                }
            }
        }
        return xr;
    }

    @Override
    public XaResult<NewsVo> createModel(News model)
            throws BusinessException {
        XaResult<NewsVo> xr = new XaResult<NewsVo>();
        News obj = newsRepository.save(model);
        xr.setObject(JSON.parseObject(JSON.toJSONString(obj), NewsVo.class));
        xr.getObject().setId(obj.getId());
        return xr;
    }

    @Override
    public XaResult<NewsVo> updateModel(News model)
            throws BusinessException {
        News obj = newsRepository.findOne(model.getId());
        XaResult<NewsVo> xr = new XaResult<NewsVo>();
        if (XaUtil.isNotEmpty(obj)) {
            obj.setTitle(XaUtil.isNotEmpty(model.getTitle()) ? model.getTitle() : obj.getTitle());
            obj.setSource(XaUtil.isNotEmpty(model.getSource()) ? model.getSource() : obj.getSource());
            obj.setThumbnail(XaUtil.isNotEmpty(model.getThumbnail()) ? model.getThumbnail() : obj.getThumbnail());
            obj.setContent(XaUtil.isNotEmpty(model.getContent()) ? model.getContent() : obj.getContent());
            obj.setIsHot(XaUtil.isNotEmpty(model.getIsHot()) ? model.getIsHot() : obj.getIsHot());
            obj.setType(XaUtil.isNotEmpty(model.getType()) ? model.getType() : obj.getType());
            obj = newsRepository.save(obj);
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj), NewsVo.class));
            xr.getObject().setId(obj.getId());
        } else {
            throw new BusinessException(XaConstant.Message.object_not_find);
        }
        return xr;
    }

}
